var uls = document.querySelector('.box_right>ul');
var right = document.querySelector('.btoptop>.right')
var id = localStorage.getItem('id')
var token = localStorage.getItem('token')

if (id && token) {


    // 修改list的连接
    // document.querySelector('.list').setAttribute('href', "list.html?token=" + token + '&id=' + id)
    // 发送ajax获取用户信息
    var xhr = new XMLHttpRequest;
    xhr.open('get', `http://localhost:8888/users/info/${id}`)
    xhr.setRequestHeader('authorization', token)
    xhr.send()
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4) {
            if (xhr.status >= 200 && xhr.status < 300) {
                var res = xhr.responseText;
                res = JSON.parse(res)
                var { code, info: { username } } = res;
                // console.log(res);
                uls.innerHTML = `
                    <li>
                        <a class="brli1">${username}</a>
                    </li>
                    <li>
                        <a data-id="${id}" data-token="${token}" href="javascript:;" class="logout">退出</a>
                    </li>
                    <li>
                        <a>我的订单</a>
                    </li>
                    <li>
                        <a>我的京东</a>
                    </li>
                    <li>
                        <a>京东会员</a>
                    </li>
                    <li>
                        <a>企业采购</a>
                    </li>
                    <li>
                        <a>客户服务</a>
                    </li>
                    <li>
                        <a>网站导航</a>
                    </li>
                    <li>
                        <a>手机京东</a>
                    </li>
                  
                `

                right.innerHTML=`
                     Hi,<a class="brli1" style="color:black;">${username}</a>
                    <ul>
                        <li style="border:none;"><a data-id="${id}" data-token="${token}" href="javascript:;" class="logout" style="color:red;">退出</a></li>
                    </ul>        
                `
            }
        }
    }
}

uls.onclick = function () {
    var target = window.event.target || window.event.srcElement;
    if (target.className === 'logout') {
        var xhr = new XMLHttpRequest;
        xhr.open('get', 'http://localhost:8888/users/logout/' + target.getAttribute('data-id'))
        xhr.setRequestHeader('authorization', target.getAttribute('token'))
        xhr.send()
        xhr.onreadystatechange = function () {
            if (xhr.readyState === 4) {
                if (xhr.status >= 200 && xhr.status < 300) {
                    var res = xhr.responseText;
                    res = JSON.parse(res)
                    console.log(res);
                    var { code, message } = res;
                    if (code === 1) {
                        alert(message)
                        // 将内容换成登录注册
                        location.href = "home.html"
                    }
                }
            }
        }
    }
}
right.onclick = function () {
    var target = window.event.target || window.event.srcElement;
    if (target.className === 'logout') {
        var xhr = new XMLHttpRequest;
        xhr.open('get', 'http://localhost:8888/users/logout/' + target.getAttribute('data-id'))
        xhr.setRequestHeader('authorization', target.getAttribute('token'))
        xhr.send()
        xhr.onreadystatechange = function () {
            if (xhr.readyState === 4) {
                if (xhr.status >= 200 && xhr.status < 300) {
                    var res = xhr.responseText;
                    res = JSON.parse(res)
                    console.log(res);
                    var { code, message } = res;
                    if (code === 1) {
                        alert(message)
                        // 将内容换成登录注册
                        location.href = "home.html"
                    }
                }
            }
        }
    }
}
